package com.poetize.plus.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poetize.plus.entity.dto.TimeLineDTO;
import com.poetize.plus.entity.dto.page.TimeLinePageDTO;
import com.poetize.plus.entity.vo.TimeLineVO;
import com.poetize.plus.entity.vo.page.TimeLinePageVO;
import com.poetize.plus.service.TimeLineService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author zuosy
 * @created 2024/5/13 14:21
 */
@Tag(name = "时间线信息")
@RestController
@RequestMapping("/admin/timeLine")
@RequiredArgsConstructor
public class TimeLineController {

    private final TimeLineService timeLineService;

    @GetMapping("/timeLinePage")
    @Operation(summary = "分页数据")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Page<TimeLinePageVO>> timeLinePage(TimeLinePageDTO timeLinePageDTO){
        return R.ok(timeLineService.timeLinePage(timeLinePageDTO));
    }

    @PostMapping("/addTimeLine")
    @Operation(summary = "新增时间线")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> addTimeLine(@RequestBody @Validated(Insert.class) TimeLineDTO timeLineDTO){
        return R.ok(timeLineService.addTimeLine(timeLineDTO));
    }

    @GetMapping("/{id}")
    @Operation(summary = "详情")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<TimeLineVO> queryById(@PathVariable("id") Long id){
        return R.ok(timeLineService.queryById(id));
    }

    @PostMapping("/updateTimeLine")
    @Operation(summary = "修改")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> updateTimeLine(@RequestBody @Validated(Update.class) TimeLineDTO timeLineDTO){
        return R.ok(timeLineService.updateTimeLine(timeLineDTO));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除")
    @PreAuthorize(value = "hasRole('administrator')")
    public R<Boolean> delete(@PathVariable("id") Long id){
        return R.ok(timeLineService.removeById(id));
    }
}
